<template>
	<view class="tools-body">
		<button type="primary" @click="onConsume">点击扫描</button>
		<view class="userInfo">
			用户昵称:{{clientInfo.nickName}}
		</view>
		<view class="tools-money">
			<view class="cu-form-group">
				<view class="title">消费金额</view>
				<input placeholder="输入消费金额" v-model="money" name="input"></input>
			</view>
		</view>
		<view class="coupon-list margin-top">
			<checkbox-group class="block" @change="CheckboxChange">
				<view class="cu-form-group" v-for="(item,index) in couponList" :key="index">
					<view class="title">{{item.coupon_name}}</view>
					<checkbox :class="item.checked?'checked':''" :checked="item.checked?true:false" :value="item.coupon_nominal"></checkbox>
				</view>
			</checkbox-group>
		</view>
		<button class="margin-top" type="primary" @click="onConsumption">确认消费</button>
	</view>
</template>

<script>
	import api from '@/api/index.js';
	export default {
		data() {
			return {
				clientInfo: {

				},
				money: 1,
				coupon: [],
				couponList: []
			}
		},
		onLoad() {
			//this.getClientInfo("oX0985QAlJycn5CayIAYcVRm7F5E");
		},
		methods: {
			onConsume() {
				var self = this;
				uni.scanCode({
					success: function(res) {
						console.log('条码内容：' + res.result);
						self.getClientInfo(res.result);
					}
				});
			},
			/**
			 * 获取用户信息
			 * @param {Object} openId
			 */
			getClientInfo(openId) {
				let data = {
					"open_id": openId
				}
				api.benwei.get_client(data).then(response => {
					this.clientInfo = response.userInfo
					this.couponList = response.coupon
					console.log("返回数据", response);
				});
			},
			/**
			 * 选择优惠券
			 * @param {Object} 
			 */
			CheckboxChange(e) {
				var active = [];
				var items = this.couponList,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					items[i].checked = false;
					for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
						console.log(values[j]);
						if (items[i].coupon_nominal == values[j]) {
							items[i].checked = true;
							active.push(items[i]._id)
							break
						}
					}
				}
				this.coupon = active;
			},
			/**
			 * 确认消费
			 */
			onConsumption() {
				var _data = {
					money: this.money,
					coupon: this.coupon,
					open_id: this.clientInfo.openId
				}
				console.log("_data", _data);
				api.benwei.on_settle_accounts(_data).then(response => {
					uni.showToast({
						title: '消费成功',
						duration: 2000,
						success: function() {
							uni.navigateBack()
						}
					});

					console.log("response", response);
				});

			}
		}
	}
</script>

<style>
	.tools-money {
		margin-top: 30rpx;
	}

	.tools-body {
		padding: 50rpx;
	}

	.userInfo {
		padding: 30rpx;
		font-size: 14px;
	}
</style>
